package com.example.base.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.base.entity.Course;
import com.example.base.entity.Type;
import com.example.base.service.CourseService;
import com.example.base.util.Response;
import com.example.base.util.validator.CourseCtrlValidate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    CourseService courseService;

    @GetMapping("/get")
    public Response getCourse(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "size",required = false) Integer size,
            @RequestParam(value = "spId",required = true) String spId,
            Course course){
        if (ObjectUtils.isEmpty(page)) {
            page = 1;
        }
        if (ObjectUtils.isEmpty(size)) {
            size = 5;
        }

        IPage<Course> courseIPage = courseService.selectCourse(course, page, size);
        courseIPage.setTotal(courseIPage.getTotal());


        Response response = new Response();
        response.setData(courseIPage);
        response.setMsg("课程查询成功");
        response.setStatus(200);

        return response;
    }

    @GetMapping("/getAll")
    public Response getAllSkill(){

        List<Course> list = courseService.selectAllCourse();

        Response response = new Response();
        response.setData(list);
        response.setMsg("所有课程查询成功");
        response.setStatus(200);

        return response;
    }

    @PostMapping("/add")
    public Response addCourse(@RequestBody Course course){
        // 默认新添加课程为1下架状态
        course.setStatus("1");

        // 设置添加日期
        Date now = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        course.setCreateTime(simpleDateFormat.format(now));

        Response response = new Response();
        CourseCtrlValidate.addCourseValidate(course, response);

        if (ObjectUtils.isNotEmpty(response.getError())){
            return response;
        }
        int statusNum = courseService.addCourse(course);
        response.setData(statusNum);
        return response;
    }

    @PutMapping("/update")
    public Response updateCourse(@RequestBody Course course){
        // 默认修改课程为1下架状态
        course.setStatus("1");

        // 设置修改日期
        Date now = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        course.setUpdateTime(simpleDateFormat.format(now));

        Response response = new Response();
        CourseCtrlValidate.updateCourseValidate(course, response);

        if (ObjectUtils.isNotEmpty(response.getError())){
            return response;
        }

        int statusNum = courseService.updateCourse(course);
        if (statusNum == 0){
            response.setMsg("执行修改命令成功，但没有此courseId");
        }
        response.setData(statusNum);
        return response;
    }

    @PutMapping("/del")
    public Response delCourse(@RequestParam(value = "courseId", required = true) String courseId){
        Response response = new Response();
        int statusNum = courseService.delCourse(courseId);
        if (statusNum == 0){
            response.setMsg("执行删除命令成功，但没有此courseId");
        }else {
            response.setMsg("执行删除命令成功");
        }
        response.setStatus(200);
        response.setData(statusNum);
        return response;
    }
}
